package com.hpu.tools;

import com.hpu.listener.DownloadListener;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class NoticeTools {

	public static void downloadFile(DownloadListener listener,String savePath,String fileName,byte[] bytes){
		File file = new File(savePath+"/"+fileName);
        int length=bytes.length;
        OutputStream out;
		try {
			out = new FileOutputStream(file);
			InputStream is = new ByteArrayInputStream(bytes);
	        byte[] buff = new byte[1024];
	        int len = 0;
	        int now=0;
	        while((len=is.read(buff))!=-1){
	            out.write(buff, 0, len);
	            now+=len;
	            if(listener!=null)
	            listener.onDownload(length,now);
	        }
	        is.close();
	        out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
        
	}
}
